package com.gehj.androidlibs;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.SuperscriptSpan;

import com.gehj.commonui.ClockView;
import com.gehj.commonui.ClockView_AirVolume;
import com.gehj.commonui.ClockView_test;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DashBroadActivity extends AppCompatActivity {
    Float value;
    private ClockView_test clock_view;
    private ClockView_AirVolume clock_view_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_broad);

        clock_view =  findViewById(R.id.dashboard1);
              //设置指针最终位置，附带动画效果
        clock_view.setCompleteDegree(10.0f);

        clock_view_2 = findViewById(R.id.db_2); //风量


        /*
        SpannableStringBuilder spannableStringBuilder
                = new SpannableStringBuilder(getSuperScript("m3"));
        spannableStringBuilder.append("/min");
        clock_view_2.setTitleDial(getSuperScript("m3").toString());*/




        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                value = new Random().nextFloat()*100;
                Message message = Message.obtain();
                message.obj = value;
                message.what  = 0;
                handler.sendMessage(message);
            }
        },1500,1500);
    }


    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0){
                super.handleMessage(msg);
                float f = (float) msg.obj;
                clock_view.setCompleteDegree((float)f);
                clock_view_2.setCompleteDegree((float)f);
            }

        }
    };

    /**
     * 转字符串为上标
     * @param param
     * @return
     */
    private SpannableString getSuperScript(String param) {
        SpannableString m2 = new SpannableString(param);
        m2.setSpan(new RelativeSizeSpan(0.5f), 1, 2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);//一半大小
        m2.setSpan(new SuperscriptSpan(), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//上标
        return m2;
    }




}